package site.lanmushan.framework.datascope.interceptor;

import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.statement.Statement;
import org.springframework.context.annotation.Configuration;
import site.lanmushan.framework.datascope.annotation.DataControl;
import site.lanmushan.framework.datascope.bus.DataScopeBus;
import site.lanmushan.framework.datascope.interceptor.autoupdate.AutoUpdateInterceptor;
import site.lanmushan.framework.datascope.interceptor.datascope.DataScopeInterceptor;
import site.lanmushan.framework.datascope.support.SlashDataControlInterceptor;

import java.util.List;

/**
 * 默认
 *
 * @author Administrator
 */
@Slf4j
@Configuration
public class DefaultDataControlSlashDataControl extends AbstractISlashDataControl {


    @Override
    public Statement doInsert(Statement statement) throws JSQLParserException {
        return doInvokes(statement, insertInterceptorList);
    }

    @Override
    public Statement doSelect(Statement statement) throws JSQLParserException {
        return doInvokes(statement, selectInterceptorList);
    }

    @Override
    public Statement doUpdate(Statement statement) throws JSQLParserException {
        return doInvokes(statement, updateInterceptorList);

    }

    @Override
    public Statement doDelete(Statement statement) throws JSQLParserException {
        return doInvokes(statement, deleteInterceptorList);

    }

    private Statement doInvokes(Statement statement, List<SlashDataControlInterceptor> interceptorList) throws JSQLParserException {
        Statement tempStatement = statement;
        for (SlashDataControlInterceptor interceptor : interceptorList) {
            if (interceptor instanceof DataScopeInterceptor) {
                if (DataScopeBus.isEnable()) {
                    tempStatement = interceptor.doIntercept(tempStatement);
                }
            } else if(interceptor instanceof AutoUpdateInterceptor){
                  tempStatement = interceptor.doIntercept(tempStatement);
            }
        }
        return tempStatement;
    }

}
